package uc.allapp;

import java.text.Collator;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import cn.onekit.CALLBACK;
import cn.onekit.JSON;
import cn.onekit.android.ACTIVITY;
import uc.IconView;

public class SectionAdapter extends BaseAdapter {
	public Map<String, List<ResolveInfo>> allApp = new HashMap<String, List<ResolveInfo>>();
	JSONArray appIDs =new JSONArray();
	private Context mContext;
	CALLBACK<String> callback;

	public SectionAdapter(Context mContext, Map<String, List<ResolveInfo>> allApp, 
			CALLBACK<String> callback) {
		this.mContext = mContext;
		this.callback = callback;
		this.allApp = allApp;
	}
	public void setData(JSONArray appIDs) {
		this.appIDs = appIDs;
	}
	public int getCount() {
		return allApp.keySet().size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	@SuppressLint("InflateParams")
	public View getView(final int position, View view, ViewGroup arg2) {

		final PackageManager packageManager = mContext.getPackageManager();
		AllAppSection viewHolder;
		if (view == null) {
			viewHolder = new AllAppSection(mContext);
			viewHolder.appGridView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
					if (callback == null) {
						return;
					}
					IconView iconView = (IconView) arg1;
					boolean select = !iconView.getSelect();
					iconView.setSelect(select);
					callback.run(select, (String) arg1.getTag());
				}

			});
		} else {
			viewHolder = (AllAppSection) view;
		}

		Set<String> keySet = allApp.keySet();
		String[] indexes = keySet.toArray(new String[keySet.size()]);
		Arrays.sort(indexes, Collator.getInstance(java.util.Locale.CHINA));
		String index = indexes[position];
		final List<ResolveInfo> apps = allApp.get(index);
		viewHolder.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
				ACTIVITY.dp2px(IconView.HEIGHT) * (int) Math.ceil(apps.size() / 4)));
		viewHolder.indexTextView.setText(index);
		viewHolder.appGridView.setAdapter(new BaseAdapter() {

			@Override
			public int getCount() {
				return apps.size();
			}

			@Override
			public Object getItem(int position) {
				return null;
			}

			@Override
			public long getItemId(int position) {
				return 0;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				IconView iconView;
				if (convertView == null) {
					iconView = new IconView(mContext);
					iconView.setLayoutParams(
							new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, ACTIVITY.dp2px(IconView.HEIGHT)));
				} else {
					iconView = (IconView) convertView;
				}
				ResolveInfo app = apps.get(position);
				iconView.setTag(app.activityInfo.packageName);
				String appID = app.activityInfo.packageName;
				Drawable icon = app.activityInfo.loadIcon(packageManager);
				String title = (String) packageManager.getApplicationLabel(app.activityInfo.applicationInfo);
				iconView.setData(icon, title);
				iconView.setSelect(JSON.contains(appIDs,appID));
				return iconView;
			}

		});
		return viewHolder;

	}

}